import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_tools/chart_tools.dart';
import 'package:x3_charts_core/model/general_chart_model.dart';

///**
///* @author hrongyong
///* @date   2/4/21
///* @email hrongyong@gmail.com
///* Description:
///*/
const double _kDistanceLeft = 2; // 距离左边的距离(最左直方于Y轴线的距离)

class NewGenericStackedChartPainter extends CustomPainter {
  List<GeneralChartModel> chartList;

  ///背景颜色
  Color bgColor;

  ///底坐标颜色
  Color bottomCoordinateColor;

  ///第一个直方图的颜色
  final Color firstHistogramColor;

  ///第一个直方图描述的前缀
  final String firstHistogramDescriptionPrefix;

  ///第二个直方图的颜色
  final Color secondHistogramColor;

  ///第二个直方图描述的前缀
  final String secondHistogramDescriptionPrefix;

  ///第三个直方图的颜色
  final Color thirdHistogramColor;

  ///第三个直方图描述的前缀
  final String thirdHistogramDescriptionPrefix;

  ///第一个折线的颜色
  final Color firstPolylineColor;

  ///第一个折线描述的前缀
  final String firstPolylineDescriptionPrefix;

  ///第二个折线的颜色
  final Color secondPolylineColor;

  ///第二个折线描述的前缀
  final String secondPolylineDescriptionPrefix;

  ///异常颜色-目前只用于直方图
  Color abnormalColor;

  ///两个直方图之间的间距
  double spacing;

  ///是否要绘画x轴name上面的刻度线
  bool drawXTickMark;

  ///是否要显示数据
  bool isDisplayData;

  ///左边Y轴最大值
  final int leftYAxisMax;

  ///左边Y轴的单位
  final String leftYAxisUnit;

  ///右边Y轴最大值
  final int rightYAxisMax;

  ///右边Y轴的单位
  final String rightYAxisUnit;

  ///滑动事件，点击事件 {"dx":dx,"index":index}
  ValueNotifier<Map<String, dynamic>> notifier;

  ///最大显示多少个直方
  final int maxItemCount;

  ///间距
  final EdgeInsets chartPadding;

  ///底坐标文本大小
  final double bottomCoordinateTextSize;

  ///文字距离坐标轴/刻度线的距离
  final double textDistanceAxisSize;

  NewGenericStackedChartPainter({
    @required this.chartList,
    @required this.notifier,
    @required this.chartPadding,
    this.firstHistogramColor = Colors.blue,
    this.firstHistogramDescriptionPrefix = "",
    this.secondHistogramColor = Colors.lightBlue,
    this.secondHistogramDescriptionPrefix = "",
    this.thirdHistogramColor = Colors.lightBlueAccent,
    this.thirdHistogramDescriptionPrefix = "",
    this.firstPolylineColor = Colors.orange,
    this.firstPolylineDescriptionPrefix = "",
    this.secondPolylineColor = Colors.orangeAccent,
    this.secondPolylineDescriptionPrefix = "",
    this.bgColor = Colors.white,
    this.bottomCoordinateColor = Colors.white,
    this.spacing = 10.0,
    this.drawXTickMark = false,
    this.isDisplayData = false,
    this.abnormalColor = const Color.fromARGB(255, 194, 194, 194),
    this.leftYAxisMax,
    this.leftYAxisUnit = "",
    this.rightYAxisUnit = "",
    this.rightYAxisMax,
    this.maxItemCount = 14,
    this.bottomCoordinateTextSize = 12.0,
    this.textDistanceAxisSize = 4,
  }) : super(repaint: notifier);

  double maxWidth = 0.0;
  double maxHeight = 0.0;
  double xStep = 0.0; // x 间隔
  double yStep = 0.0; // y 间隔
  //直方图直方最大高度
  double maxHistogramHeight = 0.0;

  ///轴线画笔
  Paint axisPaint = Paint()
    ..strokeWidth = 1.0
    ..color = Colors.black12;

  //直方图的填充颜色
  Paint fillPaint = Paint()..color = Colors.red;

  //折线
  Paint linePaint = Paint()
    ..color = Colors.red
    ..strokeCap = StrokeCap.round
    ..strokeJoin = StrokeJoin.round
    ..style = PaintingStyle.stroke;
  final TextPainter _textPainter = TextPainter(textDirection: TextDirection.ltr);
  final TextPainter _textPainter01 = TextPainter(textDirection: TextDirection.ltr); //第一个直方图
  final TextPainter _textPainter02 = TextPainter(textDirection: TextDirection.ltr); //第二个直方图
  final TextPainter _textPainter03 = TextPainter(textDirection: TextDirection.ltr); //第三个直方图
  final TextPainter _textPainter04 = TextPainter(textDirection: TextDirection.ltr); //第一根折线
  final TextPainter _textPainter05 = TextPainter(textDirection: TextDirection.ltr); //第二根折线

  final List<Offset> firstPolyLineOffset = []; // 折线点位信息
  final List<Offset> secondPolylineOffset = []; // 折线点位信息

  @override
  void paint(Canvas canvas, Size size) {
    if (chartList.length == 0) return;

    ///裁剪显示的区域
    canvas.clipRect(Offset.zero & size);
    ///绘制背景
    canvas.drawRect(Offset.zero & size, Paint()..color = bgColor);

    ///确定底坐标大小
    TextPainter painter01 = TextPainter(
        maxLines: 1,
        textDirection: TextDirection.ltr,
        text: TextSpan(
            text: "test",
            style: TextStyle(
              fontSize: bottomCoordinateTextSize,
            )));
    painter01.layout();

    ///可绘画的宽度
    maxWidth = size.width - chartPadding.left - chartPadding.right;

    /// x 间隔
    xStep = maxWidth / (chartList.length > maxItemCount ? maxItemCount : chartList.length);

    ///可绘画的高度
    maxHeight = size.height - chartPadding.top - chartPadding.bottom - painter01.height - textDistanceAxisSize;

    /// y 间隔
    yStep = maxHeight / 10;

    //移动画笔中心
    //直方图直方最大高度 = size高度 - 底部预留 - 顶部预留
    maxHistogramHeight = size.height - chartPadding.top - chartPadding.bottom - painter01.height;

    ///移动绘画中心
    canvas.translate(chartPadding.left, size.height - chartPadding.bottom - textDistanceAxisSize);
    canvas.translate(-notifier.value['dx'], 0);
    //绘制X底坐标
    canvas.drawLine(
      Offset.zero,
      Offset(maxWidth, 0),
      Paint()
        ..color = bottomCoordinateColor
        ..strokeWidth = 1,
    );
    //绘制X轴文本
    drawXText(canvas, size);

    ///绘制左边Y轴刻度和文本
    if (leftYAxisMax != null) drawLeftYAxisScaleAndText(canvas, size);

    ///绘制右边Y轴刻度和文本
    if (rightYAxisMax != null) drawRightYAxisScaleAndText(canvas, size);

    //绘制第一个直方图，也是叠块最下面的直方图
    drawFirstHistogramChart(canvas, size);
    //绘制第二个直方图，也是叠块最中间的直方图
    drawSecondHistogramChart(canvas, size);
    //绘制第三个直方图，也是叠块最下面的直方图
    drawThirdHistogramChart(canvas, size);

    //开始绘制折线图
    canvas.translate(xStep, 0);
    //生成折线图的点位
    collectPoints(canvas, size);

    ///是否要绘制第一根折线
    double firstValue = 0.0;
    firstPolyLineOffset.forEach((element) {
      firstValue = element.dy == 0.0 ? firstValue : element.dy;
    });

    ///是否要绘制第二根折线
    double secondValue = 0;
    secondPolylineOffset.forEach((element) {
      secondValue = element.dy == 0.0 ? secondValue : element.dy;
    });
    //绘制点位的圆点
    drawDots(canvas, size, first: firstValue != 0.0, second: secondValue != 0.0);
    //绘制点位上面的文本
    if (isDisplayData) drawTheTextAboveThePoint(canvas, size, first: firstValue != 0.0, second: secondValue != 0.0);
    //绘制折线
    if (firstValue != 0.0) {
      drawLineChart(canvas: canvas, line: firstPolyLineOffset, lineColor: firstPolylineColor);
    }
    if (secondValue != 0.0) drawLineChart(canvas: canvas, line: secondPolylineOffset, lineColor: secondPolylineColor);

    ///绘制点击显示的弹窗
    if (notifier.value['index'] != null) drawThePopUpWindowDisplayedByClicking(canvas, size);
  }

  drawThePopUpWindowDisplayedByClicking(Canvas canvas, Size size) {
    canvas.save();
    double titleFontSize = 12.0;
    double subTitleFontSize = 12.0;
    int _index = notifier.value['index'];
    double _dx = notifier.value['dx'];
    TextSpan text00 = TextSpan(
      text: chartList[_index].name,
      style: TextStyle(fontSize: titleFontSize, color: Color(0xFF333333)),
    );
    //第一个直方图的描述
    TextSpan text01;
    if (chartList[_index].firstHistogramDescription != null && chartList[_index].firstHistogramDescription != "")
      text01 = TextSpan(
        text: firstHistogramDescriptionPrefix + chartList[_index].firstHistogramDescription,
        style: TextStyle(fontSize: subTitleFontSize, color: Color(0xFF333333)),
      );
    //第二个直方图的描述
    TextSpan text02;
    if (chartList[_index].secondHistogramDescription != null && chartList[_index].secondHistogramDescription != "")
      text02 = TextSpan(
        text: secondHistogramDescriptionPrefix + chartList[_index].secondHistogramDescription,
        style: TextStyle(fontSize: subTitleFontSize, color: Color(0xFF333333)),
      );
    //第三个直方图的描述
    TextSpan text03;
    if (chartList[_index].thirdHistogramDescription != null && chartList[_index].thirdHistogramDescription != "")
      text03 = TextSpan(
        text: thirdHistogramDescriptionPrefix + chartList[_index].thirdHistogramDescription,
        style: TextStyle(fontSize: subTitleFontSize, color: Color(0xFF333333)),
      );
    //第一根折线的描述
    TextSpan text04;
    if (chartList[_index].firstPolylineDescription != null && chartList[_index].firstPolylineDescription != "")
      text04 = TextSpan(
        text: firstPolylineDescriptionPrefix + chartList[_index].firstPolylineDescription,
        style: TextStyle(fontSize: subTitleFontSize, color: Color(0xFF333333)),
      );
    //第二根折线的描述
    TextSpan text05;
    if (chartList[_index].secondPolylineDescription != null && chartList[_index].secondPolylineDescription != "")
      text05 = TextSpan(
        text: secondPolylineDescriptionPrefix + chartList[_index].secondPolylineDescription,
        style: TextStyle(fontSize: subTitleFontSize, color: Color(0xFF333333)),
      );

    _textPainter.text = text00;
    if (text01 != null) _textPainter01.text = text01;
    if (text02 != null) _textPainter02.text = text02;
    if (text03 != null) _textPainter03.text = text03;
    if (text04 != null) _textPainter04.text = text04;
    if (text05 != null) _textPainter05.text = text05;
    _textPainter.layout(); // 进行布局
    if (text01 != null) _textPainter01.layout(); // 进行布局
    if (text02 != null) _textPainter02.layout(); // 进行布局
    if (text03 != null) _textPainter03.layout(); // 进行布局
    if (text04 != null) _textPainter04.layout(); // 进行布局
    if (text05 != null) _textPainter05.layout(); // 进行布局

    Size size00 = _textPainter.size;
    Size size01;
    if (text01 != null) size01 = _textPainter01.size;
    Size size02;
    if (text02 != null) size02 = _textPainter02.size;
    Size size03;
    if (text03 != null) size03 = _textPainter03.size;
    Size size04;
    if (text04 != null) size04 = _textPainter04.size;
    Size size05;
    if (text05 != null) size05 = _textPainter05.size;

    ///最大宽度
    double maxTextWidth = ChartTools.getTheMaximum(a: size00, b: size01, c: size02, d: size03, e: size04, f: size05);

    ///最大高度
    double maxTextHeight = ChartTools.getTheTotal(a: size00, b: size01, c: size02, d: size03, e: size04, f: size05, spacing: 4);
    double _l = size.width / 2 + _dx - xStep - maxTextWidth / 2;
    double _t = -size.height + 20;
    double _r = size.width / 2 + _dx - xStep + maxTextWidth / 2;
    double _b = -size.height + 20 + maxTextHeight + 20;

    ///底部弹框
    Rect rectFromLTRB = Rect.fromLTRB(_l - 20, _t, _r + 20, _b);

    canvas.drawRRect(RRect.fromRectXY(rectFromLTRB, 8, 8), Paint()..color = Colors.white.withOpacity(0.9));
    canvas.drawRRect(
        RRect.fromRectXY(rectFromLTRB, 8, 8),
        Paint()
          ..color = Colors.grey.withOpacity(0.1)
          ..strokeWidth = 1
          ..style = PaintingStyle.stroke);

    ///中间显示的数据
    //积累高度
    double totalHeight = -size.height + 20 + 10;
    Offset offset00 = Offset(_l - 10, totalHeight);

    Offset offset01;
    if (size00 != null) totalHeight += size00.height + 4;
    if (text01 != null) {
      offset01 = Offset(_l + 10, totalHeight);
    }

    Offset offset02;
    if (text01 != null) totalHeight += size01.height + 4;

    if (text02 != null) {
      offset02 = Offset(_l + 10, totalHeight);
    }

    Offset offset03;
    if (text02 != null) totalHeight += size02.height + 4;

    if (text03 != null) {
      offset03 = Offset(_l + 10, totalHeight);
    }

    Offset offset04;
    if (text03 != null) totalHeight += size03.height + 4;

    if (text04 != null) {
      offset04 = Offset(_l + 10, totalHeight);
    }

    Offset offset05;
    if (text04 != null) totalHeight += size04.height + 4;

    if (text05 != null) {
      offset05 = Offset(_l + 10, totalHeight);
    }
    _textPainter.paint(canvas, offset00);
    if (offset01 != null) {
      canvas.drawCircle(Offset(offset01.dx - 10, offset01.dy + _textPainter01.height / 2), 5, Paint()..color = firstHistogramColor);
      _textPainter01.paint(canvas, offset01);
    }
    if (offset02 != null) {
      canvas.drawCircle(Offset(offset02.dx - 10, offset02.dy + _textPainter02.height / 2), 5, Paint()..color = secondHistogramColor);
      _textPainter02.paint(canvas, offset02);
    }
    if (offset03 != null) {
      canvas.drawCircle(Offset(offset03.dx - 10, offset03.dy + _textPainter03.height / 2), 5, Paint()..color = thirdHistogramColor);
      _textPainter03.paint(canvas, offset03);
    }
    if (offset04 != null) {
      canvas.drawCircle(Offset(offset04.dx - 10, offset04.dy + _textPainter04.height / 2), 5, Paint()..color = firstPolylineColor);
      _textPainter04.paint(canvas, offset04);
    }
    if (offset05 != null) {
      canvas.drawCircle(Offset(offset05.dx - 10, offset05.dy + _textPainter05.height / 2), 5, Paint()..color = secondPolylineColor);
      _textPainter05.paint(canvas, offset05);
    }
    canvas.restore();
  }

  drawRightYAxisScaleAndText(Canvas canvas, Size size) {
    canvas.save();
    canvas.translate(maxWidth, 0);

    ///Y轴线
    for (int i = 0; i <= 10; i++) {
      canvas.drawLine(Offset.zero, Offset(-4, 0), axisPaint);
      _drawRightYAxisText(canvas, title: (rightYAxisMax / 10 * i).toInt().toString() + rightYAxisUnit, offset: Offset.zero);
      canvas.translate(0, -yStep);
    }

    canvas.restore();
  }

  ///绘制右边Y轴文字
  void _drawRightYAxisText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    double fontSize = 6,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx, offset.dy - textSize.height / 2);
    _textPainter.paint(canvas, textOffsetPos);
  }

  drawLeftYAxisScaleAndText(Canvas canvas, Size size) {
    canvas.save();

    ///Y轴线
    for (int i = 0; i <= 10; i++) {
      canvas.drawLine(Offset.zero, Offset(-4, 0), axisPaint..color);
      _drawLeftYAxisText(canvas, title: (leftYAxisMax / 10 * i).toInt().toString() + leftYAxisUnit, offset: Offset.zero);
      canvas.translate(0, -yStep);
    }

    canvas.restore();
  }

  ///绘制左边Y轴文字
  void _drawLeftYAxisText(
    Canvas canvas, {
    String title,
    Offset offset = Offset.zero,
    double fontSize = 6,
    Color color = Colors.black,
  }) {
    ///绘制标题
    TextSpan text = TextSpan(text: title, style: TextStyle(fontSize: fontSize, color: color));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局
    Size textSize = _textPainter.size;
    Offset textOffsetPos = Offset(offset.dx - textSize.width - 4, offset.dy - textSize.height / 2);
    _textPainter.paint(canvas, textOffsetPos);
  }

  drawLineChart({@required Canvas canvas, @required List<Offset> line, @required Color lineColor}) {
    Offset p1 = line[0];
    Path path = Path()..moveTo(p1.dx, p1.dy);
    for (var i = 1; i < line.length; i++) {
      if (line[i].dy != -1) {
        path.lineTo(line[i].dx, line[i].dy);
      }
    }
    linePaint..strokeWidth = 1;
    linePaint..color = lineColor;
    PathMetrics pms = path.computeMetrics();
    pms.forEach((pm) {
      canvas.drawPath(pm.extractPath(0, pm.length * notifier.value['progress']), linePaint);
    });
  }

  drawTheTextAboveThePoint(Canvas canvas, Size size, {bool first = false, bool second = false}) {
    if (first) {
      for (int i = 0; i < chartList.length; i++) {
        if (firstPolyLineOffset[i].dy != -1) {
          _drawAxisText(
            canvas,
            chartList[i].firstPolylineDescription,
            alignment: Alignment.center,
            fontSize: 7,
            isABrokenLinePoint: true,
            offset: firstPolyLineOffset[i],
          );
        }
      }
    }
    if (second) {
      for (int i = 0; i < chartList.length; i++) {
        if (secondPolylineOffset[i].dy != -1) {
          _drawAxisText(
            canvas,
            chartList[i].secondHistogramDescription,
            alignment: Alignment.center,
            fontSize: 7,
            isABrokenLinePoint: true,
            offset: secondPolylineOffset[i],
          );
        }
      }
    }
  }

  drawDots(Canvas canvas, Size size, {bool first = false, bool second = false}) {
    if (first) {
      ///这里是已经确定要绘制折线图了
      for (int i = 0; i < chartList.length; i++) {
        canvas.drawCircle(firstPolyLineOffset[i], 2, Paint()..color = firstPolylineColor);
      }
    }
    if (second) {
      ///这里是已经确定要绘制折线图了
      for (int i = 0; i < chartList.length; i++) {
        canvas.drawCircle(secondPolylineOffset[i], 2, Paint()..color = secondPolylineColor);
      }
    }
  }

  collectPoints(Canvas canvas, Size size) {
    firstPolyLineOffset.clear();
    secondPolylineOffset.clear();
    for (int i = 0; i < chartList.length; i++) {
      firstPolyLineOffset.add(_generatePointsOffset(lineValue: chartList[i].firstPolyline, index: i));
      secondPolylineOffset.add(_generatePointsOffset(lineValue: chartList[i].secondPolyline, index: i));
    }
  }

  Offset _generatePointsOffset({@required double lineValue, @required int index}) {
    Offset returnOffset;
    if (lineValue != null) {
      double dataHeigh = -(lineValue * maxHistogramHeight);
      returnOffset = Offset(xStep * index - xStep / 2, dataHeigh);
    } else {
      returnOffset = Offset(xStep * index - xStep / 2, -1);
    }
    return returnOffset;
  }

  drawFirstHistogramChart(Canvas canvas, Size size) {
    fillPaint..color = firstHistogramColor;
    canvas.save();
    canvas.translate(xStep, 0);

    for (int i = 0; i < chartList.length; i++) {
      if (chartList[i].firstHistogram != null && chartList[i].firstHistogram != 0.0) {
        fillPaint..color = firstHistogramColor;
        if (chartList[i].firstHistogramColor != null) {
          fillPaint..color = chartList[i].firstHistogramColor;
        }
        double firstDataHeight = -chartList[i].firstHistogram * maxHistogramHeight;

        ///绘制直方
        canvas.drawRect(
          Rect.fromLTWH(_kDistanceLeft, 0, xStep - 2 * _kDistanceLeft, firstDataHeight * notifier.value['progress']).translate(-xStep, 0),
          fillPaint,
        );

        ///绘制直方里面的文本
        if (this.isDisplayData && chartList[i].firstHistogramDescription != null && chartList[i].firstHistogramDescription != "") {
          _drawAxisText(
            canvas,
            chartList[i].firstHistogramDescription,
            alignment: Alignment.center,
            offset: Offset(-xStep / 2, (firstDataHeight / 2 - 5) * notifier.value['progress']),
            color: Colors.black,
            fontSize: 7,
          );
        }
      }
      canvas.translate(xStep, 0);
    }

    canvas.restore();
  }

  drawSecondHistogramChart(Canvas canvas, Size size) {
    fillPaint..color = secondHistogramColor;
    canvas.save();
    canvas.translate(xStep, 0);
    for (int i = 0; i < chartList.length; i++) {
      if (chartList[i].secondHistogram != null && chartList[i].secondHistogram != 0.0) {
        fillPaint..color = secondHistogramColor;
        if (chartList[i].secondHistogramColor != null) {
          fillPaint..color = chartList[i].secondHistogramColor;
        }
        double firstDataHeight = -chartList[i].firstHistogram * maxHistogramHeight;
        double secondDataHeight = -chartList[i].secondHistogram * maxHistogramHeight;
        canvas.drawRect(
          Rect.fromLTWH(_kDistanceLeft, firstDataHeight * notifier.value['progress'], xStep - 2 * _kDistanceLeft, secondDataHeight * notifier.value['progress']).translate(-xStep, 0),
          fillPaint,
        );
        if (this.isDisplayData && (chartList[i].secondHistogramDescription != null || chartList[i].secondHistogramDescription != "")) {
          _drawAxisText(
            canvas,
            chartList[i].secondHistogramDescription,
            alignment: Alignment.center,
            offset: Offset(-xStep / 2, (secondDataHeight / 2 + firstDataHeight) - 5) * notifier.value['progress'],
            color: Colors.black,
            fontSize: 7,
          );
        }
      }
      canvas.translate(xStep, 0);
    }

    canvas.restore();
  }

  drawThirdHistogramChart(Canvas canvas, Size size) {
    fillPaint..color = thirdHistogramColor;
    canvas.save();
    canvas.translate(xStep, 0);
    for (int i = 0; i < chartList.length; i++) {
      if (chartList[i].thirdHistogram != null && chartList[i].thirdHistogram != 0.0) {
        fillPaint..color = thirdHistogramColor;
        if (chartList[i].thirdHistogramColor != null) {
          fillPaint..color = chartList[i].thirdHistogramColor;
        }
        double firstDataHeight = -chartList[i].firstHistogram * maxHistogramHeight;
        double secondDataHeight = -chartList[i].secondHistogram * maxHistogramHeight;
        double dataHeight = -chartList[i].thirdHistogram * maxHistogramHeight;
        canvas.drawRect(
          Rect.fromLTWH(_kDistanceLeft, (firstDataHeight + secondDataHeight) * notifier.value['progress'], xStep - 2 * _kDistanceLeft, dataHeight * notifier.value['progress']).translate(-xStep, 0),
          fillPaint,
        );
        if (this.isDisplayData && (chartList[i].thirdHistogramDescription != null || chartList[i].thirdHistogramDescription != "")) {
          _drawAxisText(
            canvas,
            chartList[i].thirdHistogramDescription,
            alignment: Alignment.center,
            offset: Offset(-xStep / 2, (secondDataHeight + firstDataHeight + dataHeight / 2) * notifier.value['progress']),
            color: Colors.black,
            fontSize: 7,
          );
        }
      }
      canvas.translate(xStep, 0);
    }

    canvas.restore();
  }

  drawXText(Canvas canvas, Size size) {
    canvas.save();
    canvas.translate(xStep, 0);
    for (int i = 0; i < chartList.length; i++) {
      if (this.drawXTickMark) {
        canvas.drawLine(
            Offset(-xStep / 2, 0),
            Offset(-xStep / 2, 3),
            Paint()
              ..color = bottomCoordinateColor
              ..strokeWidth = 1);
      }
      _drawXAxisText(canvas, chartList[i].name, offset: Offset(-xStep / 2, this.drawXTickMark ? (3 + textDistanceAxisSize) : textDistanceAxisSize));
      canvas.translate(xStep, 0);
    }
    canvas.restore();
  }

  void _drawXAxisText(
    Canvas canvas,
    String str, {
    Offset offset = Offset.zero,
  }) {
    TextSpan text = TextSpan(text: str, style: TextStyle(fontSize: bottomCoordinateTextSize, color: Color(0xFF868686)));
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局

    Size size = _textPainter.size;

    Offset offsetPos = Offset(offset.dx - size.width / 2, offset.dy);
    _textPainter.paint(canvas, offsetPos);
  }

  void _drawAxisText(Canvas canvas, String str,
      {Color color = Colors.black,
      FontWeight fontWeight,
      double fontSize = 10,
      Alignment alignment = Alignment.centerRight,

      /// 是折线点位
      bool isABrokenLinePoint = false,
      Offset offset = Offset.zero}) {
    TextSpan text = TextSpan(
      text: str,
      style: fontWeight == null
          ? TextStyle(fontSize: fontSize, color: color)
          : TextStyle(
              fontSize: fontSize,
              color: color,
              fontWeight: fontWeight,
            ),
    );
    _textPainter.text = text;
    _textPainter.layout(); // 进行布局

    Size size = _textPainter.size;

    Offset offsetPos;
    if (isABrokenLinePoint) {
      offsetPos = Offset(offset.dx, offset.dy).translate(-size.width / 2, -size.height - 2);
    } else {
      offsetPos = Offset(-size.width / 2, -size.height / 2).translate(
        -size.width / 2 * alignment.x + offset.dx,
        0.0 + offset.dy + size.height / 2,
      );
    }

    _textPainter.paint(canvas, offsetPos);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
